package com.sicheng.lc.周赛.分类.栈;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/25 20:44
 */
public class 统计道路上的碰撞次数 {
    //https://leetcode.cn/problems/count-collisions-on-a-road/
    public int countCollisions(String directions) {
        // L: -1, S:0, R :1
        char top = 'A';
        int res = 0;
        int count = 0;

        for (int i = 0; i < directions.length(); i++) {
            char key = directions.charAt(i);
            if (key == 'R') {
                count++;
            }
            if (i == 0 || key == top || top == 'L' || (key == 'R' && top == 'S')) {
                top = key;
            } else {
                if (key - top == 'L' - 'R') {
                    res += 2;
                    count--;
                } else if (count == 0) {
                    res++;
                }
                res += count;
                count = 0;
                top = 'S';
            }
        }
        return res;
    }

    public static void main(String[] args) {
        统计道路上的碰撞次数 solution = new 统计道路上的碰撞次数();
        System.out.println(solution.countCollisions("SSRSSRLLRSLLRSRSSRLRRRRLLRRLSSRR"));
    }
}
